﻿<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<?php
if (!(isset($_SESSION["admin"]))) {
header("Location: index.php");
exit();
} else {
if ($_SESSION["admin"] != "true") {
header("Location: index.php");
exit();
}
}
?>
<?php
require("dbInc.php");
$mysqliObj = new mysqli($dbHost, $dbUser, $dbPass, $dbDB);
$msg = "";
$numCats = 0;
$catArray = array();
if (isset($_POST["btnAdd"])) {
if (strlen($_POST["txtName"]) > 0) {
$sql = sprintf("insert into %s (vCatName, vCatDescription) values ('%s', '%s');", $tableCats, mysqli_real_escape_string($mysqliObj, $_POST["txtName"]), mysqli_real_escape_string($mysqliObj, $_POST["txtDescription"]));
$mysqliObj->query($sql);
$msg = "Added...";
} else {
$msg = "You must enter a name for the category";
}//end of checking txtName value length
} else if (isset($_POST["btnDelete"])) {
$sql = sprintf("delete from %s where id = %s;", $tableCats, mysqli_real_escape_string($mysqliObj, $_POST["selCategory"]));
$mysqliObj->query($sql);
$sql = sprintf("delete from %s where iCatID = %s;", $tableProducts, mysqli_real_escape_string($mysqliObj, $_POST["selCategory"]));
$msg = "Deleted...";
} else if (isset($_POST["btnEdit"])) {
header("Location: category.php?id=" . $_POST["selCategory"]);
}//end of checking for form submissions
//populate cat array
$sql = sprintf("select id, vCatName from %s order by vCatName;", $tableCats);
$res = $mysqliObj->query($sql);
$res->data_seek(0);
$numCats = $res->num_rows;
if ($numCats > 0) {
while ($row = $res->fetch_assoc()) {
array_push($catArray, array($row["id"], $row["vCatName"]));
}//end of looping through rows
}//end of checking num_rows
$mysqliObj->close();
?>
<title>bzShopping Product categories</title>
<link type="text/css" rel="stylesheet" href="../styles.css" />
<?php
include("../includefunctions.inc");
?>
<script type="text/javascript" language="javascript">
function validateForm(theForm) {
with (theForm) {
if (validate_required(txtName,"You must enter a name for the category") == false)
{txtName.focus();return false}
}//end of with clause
}//end of validateForm function

function confirmDelete() {
if (confirm("Are you sure you want to remove that category, and all it's products?") == true) {
document.getElementById("hidConfirm").value = "1";
} else {
document.getElementById("hidConfirm").value = "0";
}
}//end of confirmDelete function

function validateForm2(theForm) {
with (theForm) {
if (hidConfirm.value != "1") {
return false;
}
}//end of with clause
}//end of validateForm2 function

function checkMsg() {
var msg = "<?php echo $msg; ?>";
if (msg.length > 0) {
alert(msg);
}
}//end of checkMsg function
</script>
</head>
<body onload="checkMsg();">
<iframe name="logoFrame" id="logoFrame" src="../logo.htm" border="0" height="120" align="top" frameborder="0" marginheight="0" width="100%" scrolling="no">
<a href="http://www.blindza.co.za/" target="_blank">
<img src="../logo/blindza_logo_smaller46.jpg" alt="blindZA.co.za logo - white text on black background, with white border - and red braille version hovering in front of normal text" width="317" height="103" border="0" />
</a>
</iframe>
<a href="main.php">Back to main page</a>
<h2>bzShopping Product categories (<?php echo count($catArray); ?>)</h2>
<?php
if ($msg != "") {
echo "<p class='error'>" . $msg . "</p>\n";
}
?>
<h3>Add new category</h3>
<form action="cats.php" method="post" enctype="multipart/form-data" onsubmit="return validateForm(this);">
<table align="center" border="0">
<tr>
<th align="right">Category name (required):</th>
<td>
<input type="text" name="txtName" />
</td>
</tr>
<tr>
<th align="right" valign="top">Category description:</th>
<td>
<textarea name="txtDescription" multiline="true" rows="3" cols="100"></textarea>
</td>
</tr>
<tr>
<th align="center" colspan="2">
<input type="submit" name="btnAdd" value="Add category" />
</th>
</tr>
</table>
</form>
<?php
if ($numCats > 0) {
?>
<h3>Existing categories</h3>
<form action="cats.php" method="post" enctype="multipart/form-data" onsubmit="return validateForm2(this);">
<table align="center" border="0">
<tr>
<th align="right">Select category:</th>
<td>
<select name="selCategory">
<?php
foreach ($catArray as $catRow) {
echo "<option  value=" . $catRow[0] . ">" . $catRow[1] . "</option>\n";
}//end of looping through array elements
?>
</select>
</td>
</tr>
<tr>
<th align="center" colspan="2">
<input type="hidden" id="hidConfirm" name="hidConfirm" value="0" />
<input type="submit" name="btnDelete" value="Remove category and products" onclick="confirmDelete();" /><br />
<input type="submit" name="btnEdit" value="Edit category and products" onclick="document.getElementById('hidConfirm').value='1';" />
</th>
</tr>
</table>
</form>
<?php
}//end of checking numCats
?>
</body>
</html>
